package com.example.myandroidapp;

import java.io.IOException;
import java.util.List;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class SecondActivity extends ListActivity
{
    private static final String TAG = "SecondActivity";

    private List<Person> myList;

    private ProgressDialog myProgress;

    private ListView listView;
    
    private static final int MY_REQUEST_ID = 1;

    private static final String URLString = "http://199.102.228.10/~deals/test/contacts.json";

    public class MyCustomAdapterAll extends ArrayAdapter<Person>
    {
        public MyCustomAdapterAll(Context context, int textViewResourceId, List<Person> theListAll)
        {
            super(context, textViewResourceId, theListAll);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent)
        {
            // TODO Auto-generated method stub
            // return super.getView(position, convertView, parent);

            View row = convertView;

            String strFileDate = null;

            String startDate = null;

            String startTime = null;

            if (row == null)
            {
                LayoutInflater inflater = getLayoutInflater();
                row = inflater.inflate(R.layout.row_contacts, parent, false);
            }

            TextView list_name = (TextView) row.findViewById(R.id.list_name);

            TextView list_tagline = (TextView) row.findViewById(R.id.list_tagline);

            list_name.setText(myList.get(position).getName());

            list_tagline.setText("Age : " + myList.get(position).getAge());

            ImageView contactlogo = (ImageView) row.findViewById(R.id.contactlogo);

            Drawable myAvatar = SecondActivity.this.getResources().getDrawable(R.drawable.avatar_male_72x72);

            int resID = getBaseContext().getResources().getIdentifier("avatar_male_72x72", "drawable", "com.example.com.dastest.myapp");

            contactlogo.setImageResource(resID);

            return row;
        }
    }

    public class DataLoadingTask extends AsyncTask<Void, Void, Void>
    {

        @Override
        protected void onPostExecute(Void result)
        {
            // TODO Auto-generated method stub

            Log.d(TAG, "----------------about to call display");
            display();

        }

        @Override
        protected void onPreExecute()
        {
            // TODO Auto-generated method stub

            myProgress = new ProgressDialog(SecondActivity.this);

            myProgress.setMessage("Please wait..");

            myProgress.setProgressStyle(ProgressDialog.STYLE_SPINNER);

            myProgress.setCancelable(true);

            myProgress.show();

            Log.d(TAG, "----------------about to call pre-");

            preRead();

        }

        @Override
        protected void onProgressUpdate(Void... values)
        {

            super.onProgressUpdate(values);
        }

        @Override
        protected Void doInBackground(Void... arg0)
        {
            try
            {
                read();
            } catch (IOException e)
            {
                e.printStackTrace();
            }
            return null;
        }

    }

    private void preRead()
    {
        myList = null;

        // make sure that listadapter is initialized to null
        setListAdapter(null);
    }

    private void read() throws IOException
    {
        ObjectsReader reader = new ObjectsReader();

        myList = reader.readPersons(URLString);
    }

    private void display()
    {
        if (myList != null)
        {

            MyCustomAdapterAll adapter = new MyCustomAdapterAll(this, R.layout.row_contacts, myList);
            setListAdapter(adapter);

            listView = getListView();

            listView.setTextFilterEnabled(true);

            listView.setOnItemClickListener(new OnItemClickListener()
            {
                public void onItemClick(AdapterView<?> parent, View view, int position, long id)
                {
                    Intent intent = new Intent(SecondActivity.this, PersonDetail.class);

                    MyCustomAdapterAll ca = (MyCustomAdapterAll) parent.getAdapter();

                    Bundle dataBundle = new Bundle();
                    
                    String namevalue = ca.getItem(position).getName();
                    dataBundle.putString("name", namevalue);
                    Log.d(TAG, "name : " + namevalue);
                    int agevalue = ca.getItem(position).getAge();
                    Log.d(TAG, "age------ : " + agevalue);
                    dataBundle.putInt("age", agevalue);
                    dataBundle.putInt("number", position);
                    Log.d(TAG, "position :------ " + position);
                    intent.putExtras(dataBundle);
                    int requestCode = MY_REQUEST_ID;
                    startActivityForResult(intent, requestCode);
                }
            });

        }

        myProgress.hide();

    }

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        this.setContentView(R.layout.activity_second);

        new DataLoadingTask().execute();

    }

    protected void onActivityResult(int requestCode, int resultCode,
            Intent data) {
        if (requestCode == MY_REQUEST_ID) {
            if (resultCode == RESULT_OK) 
            {
               
              // use 'myValue' return value here
                Log.d(TAG, "---------requestCode : " + requestCode);
                Log.d(TAG, "---------resultCode:  " + resultCode);
            }
        }
  }
}
